package com.qilu.controller;


import com.qilu.domain.R;
import com.qilu.domain.dto.UserListDTO;
import com.qilu.domain.entity.SysUserEntity;
import com.qilu.service.DoctorService;
import com.qilu.service.SysUserService;
import io.swagger.annotations.Api;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2023-05-07
 */
@RestController
@RequestMapping("/sys/user")
@Api(tags = "账户接口")
public class SysUserController extends BaseController {


    @Autowired
    SysUserService sysUserService;

    @Autowired
    DoctorService doctorService;

    @PostMapping("list")
    public R list(@RequestBody UserListDTO param){
        return R.success(sysUserService.getSysUser(param));
    }


    @PostMapping("add")
    public R add(@RequestBody SysUserEntity param){
        // 1. password 加密
        param.setPassword(passwordEncoder.encode(param.getPassword()));

        // 2. 添加
        boolean save = sysUserService.save(param);
        if(!save){
            return R.error("");
        }
        return R.success();
    }

    @PutMapping("modify")
    public R modify(@RequestBody SysUserEntity param){

        if(StringUtils.isNotBlank(param.getPassword())){
            param.setPassword(passwordEncoder.encode(param.getPassword()));
        }

        sysUserService.updateById(param);
        return R.success();
    }


    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable Integer id){
        sysUserService.removeById(id);
        return R.success();
    }


}
